
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;

namespace Common
{
    /// <summary>
    /// Description of ConditionDao.
    /// </summary>
    public class ConditionDao
    {
        private ConditionDao()
        {
        }

        public static int insert(Condition c)
        {
            string sql = "insert into ConditionMstr(patient_id,create_date)values(?,?);";
            OleDbParameter[] parameters = new OleDbParameter[2];
            parameters[0] = new OleDbParameter("@patient_id", OleDbType.Integer);
            parameters[0].Value = c.PatientId;
            parameters[1] = new OleDbParameter("@create_date", OleDbType.Date);
            parameters[1].Value = c.CreateDate;


            return AccessDBUtil.ExecuteInsert(sql, parameters);
        }

        public static int update(Condition c, bool includeFactor)
        {
            if (!includeFactor)
            {
                string sql = "update ConditionMstr set create_date=? where id=?";
                OleDbParameter[] parameters = new OleDbParameter[2];
                parameters[0] = new OleDbParameter("@sale_date", OleDbType.Date);
                parameters[0].Value = c.CreateDate;
                parameters[1] = new OleDbParameter("@id", OleDbType.Integer);
                parameters[1].Value = c.Id;
                return AccessDBUtil.ExecuteNonQuery(sql, parameters);
            }
            else
            {
                string sql = "update ConditionMstr set has_factor=?,factor1=?,factor2=?,factor3=?,factor4=?,factor5=?,factor6=?,factor6_desc=? where id=?";
                OleDbParameter[] parameters = new OleDbParameter[9];
                parameters[0] = new OleDbParameter("@has_factor", OleDbType.Boolean);
                parameters[0].Value = c.HasFactor;
                parameters[1] = new OleDbParameter("@factor1", OleDbType.Boolean);
                parameters[1].Value = c.Factor1;
                parameters[2] = new OleDbParameter("@factor2", OleDbType.Boolean);
                parameters[2].Value = c.Factor2;
                parameters[3] = new OleDbParameter("@factor3", OleDbType.Boolean);
                parameters[3].Value = c.Factor3;
                parameters[4] = new OleDbParameter("@factor4", OleDbType.Boolean);
                parameters[4].Value = c.Factor4;
                parameters[5] = new OleDbParameter("@factor5", OleDbType.Boolean);
                parameters[5].Value = c.Factor5;
                parameters[6] = new OleDbParameter("@factor6", OleDbType.Boolean);
                parameters[6].Value = c.Factor6;
                parameters[7] = new OleDbParameter("@factor6_desc", OleDbType.VarChar);
                parameters[7].Value = c.Factor6Desc;
                parameters[8] = new OleDbParameter("@id", OleDbType.Integer);
                parameters[8].Value = c.Id;
                return AccessDBUtil.ExecuteNonQuery(sql, parameters);
            }

        }
        public static Condition getById(int id)
        {
            string sql = "select id,patient_id,create_date,has_factor,factor1,factor2,factor3,factor4,factor5,factor6,factor6_desc from ConditionMstr where id=?";
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@id", OleDbType.Integer);
            parameters[0].Value = id;
            DataSet ds = AccessDBUtil.ExecuteQuery(sql, parameters);

            return Row2Condition(ds.Tables["ds"].Rows[0]);
        }
        public static Condition getByPatientId(int patientId)
        {
            string sql = "select id,patient_id,create_date,has_factor,factor1,factor2,factor3,factor4,factor5,factor6,factor6_desc from ConditionMstr where patient_id=?";
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@id", OleDbType.Integer);
            parameters[0].Value = patientId;
            DataSet ds = AccessDBUtil.ExecuteQuery(sql, parameters);
            if (ds.Tables["ds"].Rows.Count == 0) return null;
                return Row2Condition(ds.Tables["ds"].Rows[0]);
        }
        private static Condition Row2Condition(DataRow row)
        {
            Condition condition = new Condition();
            condition.Id = Int32.Parse(row["id"].ToString());
            condition.PatientId = Int32.Parse(row["patient_id"].ToString());
            condition.CreateDate = DateTime.Parse(row["create_date"].ToString());
            condition.HasFactor = bool.Parse(row["has_factor"].ToString());
            condition.Factor1 = bool.Parse(row["factor1"].ToString());
            condition.Factor2 = bool.Parse(row["factor2"].ToString());
            condition.Factor3 = bool.Parse(row["factor3"].ToString());
            condition.Factor4 = bool.Parse(row["factor4"].ToString());
            condition.Factor5 = bool.Parse(row["factor5"].ToString());
            condition.Factor6 = bool.Parse(row["factor6"].ToString());
            condition.Factor6Desc = row["factor6_desc"].ToString();

            return condition;
        }

    }
}
